{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "21\n"
     ]
    }
   ],
   "source": [
    "class Solution:\n",
    "\n",
    "    def reverse(self, x):   \n",
    "        sign = 1\n",
    "        num =[]\n",
    "        ret = 0\n",
    "        if x < 0:\n",
    "            x = abs(x)\n",
    "            sign = -1\n",
    "        while x > 0:\n",
    "            x, tmp = divmod(x, 10)\n",
    "            num.append(tmp)\n",
    "        length = len(num) - 1\n",
    "        for i in num:\n",
    "            ret += 10 ** length * i\n",
    "            length -= 1\n",
    "        ret *= sign\n",
    "        if -2**31 <= ret <= 2 **31 -1:\n",
    "            return ret\n",
    "        else:\n",
    "            return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "myenv3.9.13",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
